package com.yixincaipiao.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.yixincaipiao.R;
import com.yixincaipiao.utils.Strings;

/**
 * Created by Administrator on 2017/4/27.
 * dialog统一生成和管理类
 */

public class Dialogs {
    /**
     * 等待数据加载界面
     *
     * @param context
     * @return
     */
    public static Dialog getWaitDialog(Activity context) {

        return getWaitDialog(context, 0, null);
    }

    public static Dialog getWaitDialog(Activity context, int layout, String tvMessage) {
        View view;
        if (layout == 0) {
            view = context.getLayoutInflater().inflate(R.layout.waiting_layout, null);
        } else {
            view = context.getLayoutInflater().inflate(layout, null);
        }

        if (Strings.isNotEmpty(tvMessage)) {
            TextView textView = (TextView) view.findViewById(R.id.tv_message);
            textView.setText("" + tvMessage);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        return builder.create();
    }

    /**
     * 自定义dialog
     *
     * @param context
     * @param layout
     * @param bindEventView
     * @return
     */
    public static Dialog getCustomDialog(Activity context, int layout, BindEventView bindEventView) {
        View view = context.getLayoutInflater().inflate(layout, null);
        Dialog dialog = new Dialog(context);
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        Display dis = context.getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dis.getWidth() * 0.9);
        window.setAttributes(lp);
        window.setContentView(view);
        if (bindEventView != null) {
            bindEventView.bindEventView(view);
        }
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return dialog;
    }

    /**
     * getCustomDialog发放回调函数，用于实例化控件及相关事件绑定
     */
    public interface BindEventView {
        public void bindEventView(View v);
    }
}
